//
//  KKChatFunctionView.m
//  ILiveDemo
//
//  Created by liaozhenming on 2017/4/5.
//  Copyright © 2017年 liaozhenming. All rights reserved.
//

#import "KKChatFunctionView.h"

@implementation KKChatFunctionItem

+ (instancetype)kk_itemOfTitle:(NSString*)title itemImage:(UIImage*)itemImage selectedEventHandle:(KKChatFunctionItemSelectedEventHandle)selectedEventHandle {
    KKChatFunctionItem *item = [KKChatFunctionItem new];
    item.itemTitle = title;
    item.itemImage = itemImage;
    item.selectedEventHandle = selectedEventHandle;
    return item;
}
@end


#pragma mark - KKChatFunctionItemCell Class
@interface KKChatFunctionItemCell : UICollectionViewCell

@property (nonatomic, strong) UIImageView* img_item;
@property (nonatomic, strong) UILabel *lab_itemTitle;
@end

@implementation KKChatFunctionItemCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame: frame];
    if (self) {
        _img_item = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 55, 55)];
        _img_item.center = CGPointMake(CGRectGetWidth(frame)/2, CGRectGetHeight(frame)/2);
        [self.contentView addSubview: _img_item];
        _lab_itemTitle = [[UILabel alloc] initWithFrame: CGRectMake(0, CGRectGetMaxY(_img_item.frame) + 8, CGRectGetWidth(frame), 14)];
        _lab_itemTitle.textAlignment = NSTextAlignmentCenter;
        _lab_itemTitle.font = [UIFont systemFontOfSize: 14];
        _lab_itemTitle.textColor = [UIColor lightGrayColor];
        [self.contentView addSubview: _lab_itemTitle];
    }
    return self;
}

@end

#pragma mark - KKChatFunctionView Class
@interface KKChatFunctionView()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout> {
    
}

@property (nonatomic, strong) NSArray *arr_functionItems;
@property (nonatomic, strong) UICollectionView *functionCollectionView;
@end

@implementation KKChatFunctionView

+ (instancetype)kk_chatFunctionViewWithItems:(NSArray*)items {
    
    CGFloat screenWidth = CGRectGetWidth([[UIScreen mainScreen] bounds]);
    KKChatFunctionView *functionView = [[KKChatFunctionView alloc] initWithFrame: CGRectMake(0, 0, screenWidth, items.count > 4 ? 216 : 108)];
    functionView.backgroundColor = [UIColor whiteColor];
    functionView.arr_functionItems = items;
    return functionView;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame: frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
        flowLayout.itemSize = CGSizeMake(65, 90);
        flowLayout.minimumLineSpacing = 10;
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 30, 0, 30);
        
        self.functionCollectionView = [[UICollectionView alloc] initWithFrame: self.bounds collectionViewLayout: flowLayout];
        self.functionCollectionView.backgroundColor = [UIColor clearColor];
        [self.functionCollectionView registerClass: [KKChatFunctionItemCell class] forCellWithReuseIdentifier: @"KKChatFunctionItemCell"];
        self.functionCollectionView.delegate = self;
        self.functionCollectionView.dataSource = self;
        [self addSubview: self.functionCollectionView];
    }
    return self;
}

#pragma mark - UICollectionView methods

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arr_functionItems.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    KKChatFunctionItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: @"KKChatFunctionItemCell" forIndexPath: indexPath];
    KKChatFunctionItem *item = self.arr_functionItems[indexPath.row];
    cell.img_item.image = item.itemImage;
    cell.lab_itemTitle.text = item.itemTitle;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    KKChatFunctionItem *item = self.arr_functionItems[indexPath.row];
    if (item.selectedEventHandle) {
        item.selectedEventHandle();
    }
}

@end
